/*
 * The '{{{' is used marked the start of a folder and '}}}' is used marked the
 * end of the folder for vim in this file. You can delete one marker with 'zd'
 * and delete all markers with 'zE' in vim. With 'zo' or 'zc' you can open or
 * close a folder.
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <getopt.h>

#define VERSION "0.0.1"

static int stop_flag;

static void print_usage(char *name);

int main(int argc, char *argv[])
{
	int ch, lindex = 0;
	int port, g_output, g_input;

	static struct option long_options[] = {
		{"help",     no_argument,		NULL, 'h'},
		{"port",     required_argument, NULL, 'p'},
		{"output",   required_argument, NULL, 'o'},
		{"input",    no_argument,		NULL, 'i'},
		{"version",  no_argument,		NULL, 'v'},
		{0, 0, 0, 0}
	};

	while((ch = getopt_long(argc, argv, "hp:o:iv", long_options, &lindex)) != -1)
	{
		printf("opt = %c\n", ch);  
		printf("optarg = %s\n", optarg);  
		printf("optind = %d\n", optind);  
		printf("argv[optind - 1] = %s\n",  argv[optind - 1]);  
		switch (ch) 
		{
			case 'p':
				{
					char *end = NULL;

					port = strtol(optarg, &end, 10);
					if(errno == ERANGE || end == optarg || *end != '\0') {
						return -1;
					}
					printf("port = %d\n", port);
				}
				break;
			case 'o':
				printf("output -> %s\n", optarg);
				break;
			case 'i':
				printf("input\n");
				break;
			case 'v':
				printf("version %s\n", VERSION);
				break;
			case '?':
			case 'h':
				printf("\nUsage: %s [OPTIONS] ...\n", argv[0]);
				break;
			default:
				break;
		}
	}

	return 0;
}

